const express = require('express');
const { auth, authorize } = require('../middleware/auth');
const validate = require('../middleware/validate');
const userValidation = require('../validators/user.validator');
const userController = require('../controllers/user.controller');
const authValidation = require('../validators/auth.validator');
const authController = require('../controllers/auth.controller');
const { avatarUpload } = require('../utils/upload');

const router = express.Router();

// 微信小程序登录
router.post('/wx-login', validate(authValidation.wxLogin), authController.wxLogin);

// 微信小程序用户健康信息接口
router.get('/profile', auth(), validate(userValidation.getMiniprogramUserProfile), userController.getMiniprogramUserProfile);
router.post('/profile', auth(), validate(userValidation.updateMiniprogramUserProfile), userController.updateMiniprogramUserProfile);

// 用户头像上传
router.post('/avatar', auth(), avatarUpload.single('avatar'), validate(userValidation.uploadAvatar), userController.uploadAvatar);

router
  .route('/')
  .post(auth(), authorize(['manageUsers']), validate(userValidation.createUser), userController.createUser)
  .get(auth(), authorize(['getUsers']), validate(userValidation.getUsers), userController.getUsers);

router
  .route('/:userId')
  .get(auth(), authorize(['getUsers']), validate(userValidation.getUser), userController.getUser)
  .patch(auth(), authorize(['manageUsers']), validate(userValidation.updateUser), userController.updateUser)
  .delete(auth(), authorize(['manageUsers']), validate(userValidation.deleteUser), userController.deleteUser);

// 用户逻辑删除路由
router.delete(
  '/:userId/soft',
  auth(),
  authorize(['manageUsers']),
  validate(userValidation.softDeleteUser),
  userController.softDeleteUser
);

module.exports = router; 